const path = require('path')
const configs = require('./src/configs')
const proxyConfig = require('./proxy.config')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')

const resolve = (dir) => {
  return path.join(__dirname, dir)
}

const plugins = [new MonacoWebpackPlugin()]

if (configs.includePdfViewer || configs.includeMap) {
  const list = []
  if (configs.includePdfViewer) {
    list.push({
      from: resolve('/src/assets/pdf-viewer'),
      to: resolve('/dist/pdf-viewer')
    })
  }
  if (configs.includeMap) {
    list.push({
      from: resolve('/src/assets/map-assets'),
      to: resolve('/dist')
    })
  }
  plugins.push(new CopyWebpackPlugin(list))
}

module.exports = {
  publicPath: configs.publicPath,
  productionSourceMap: process.env.NODE_ENV === 'development',
  lintOnSave: process.env.NODE_ENV !== 'production',
  configureWebpack (config) {
    const sassLoader = require.resolve('sass-loader')
    config.module.rules.filter(rule => {
      return rule.test.toString().indexOf('scss') !== -1
    })
      .forEach(rule => {
        rule.oneOf.forEach(oneOfRule => {
          const sassLoaderIndex = oneOfRule.use.findIndex(item => item.loader === sassLoader)
          oneOfRule.use.splice(sassLoaderIndex, 0, { loader: require.resolve('css-unicode-loader') })
        })
      })
    return {
      resolve: {
        alias: {
          '@': resolve('src'),
          vue: resolve('./node_modules/vue'),
          quill: resolve('./node_modules/quill')
        }
      },
      plugins
    }
  },
  chainWebpack: config => {
    config
      .plugin('define')
      .tap((args) => {
        args[0].SUB_PROJECT = JSON.stringify(process.env.SUB_PROJECT || '')
        args[0].HAS_MANAGEMENT = JSON.stringify(configs.hasManagement)
        args[0].IS_NGINX = JSON.stringify(process.env.IS_NGINX || 'false')
        return args
      })
    config
      .plugin('html')
      .tap((args) => {
        args[0].title = configs.projectName
        return args
      })
    config.module
      .rule('svg')
      .exclude
      .add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include
      .add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
    config.when(process.env.NODE_ENV === 'production', (config) => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          primaryLibs: {
            name: 'primary-libs',
            priority: 20,
            test: /[\\/]node_modules[\\/](element-ui|vue|vue-router|vuex|core-js|vxe-table|xe-utils|axios)[\\/]/
          },
          analysisLibs: {
            name: 'analysis-libs',
            priority: 30,
            test: /[\\/]node_modules[\\/](bk-analysis-maker)[\\/]/
          }
        }
      })
    })
  },
  devServer: {
    proxy: proxyConfig
  },
  css: {
    loaderOptions: {
      scss: {
        prependData: '@import "~@/assets/styles/variables.scss";'
      }
    }
  },
  transpileDependencies: [
    'vue-echarts',
    'resize-detector'
  ],
  runtimeCompiler: true
}
